/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var findMode = function (root) {
    let res = [], preVal, count = 0, maxCount = 0

    function findEveryMode(root) {
        if (root === null) return
        findEveryMode(root.left)

        if (root.val === preVal) {
            count++
        } else {
            count = 1
        }
        if (count === maxCount) {
            res.push(root.val)
        }
        if (count > maxCount) {
            res = [root.val]
            maxCount = count
        }

        preVal = root.val

        findEveryMode(root.right)
        return
    }
    findEveryMode(root)
    return res
};
